package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallTopicMapper;
import org.linlinjava.litemall.db.domain.LitemallTopic;
import org.linlinjava.litemall.db.domain.LitemallTopicExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class LitemallTopicServiceImpl implements LitemallTopicService {

    @Autowired
    LitemallTopicMapper litemallTopicMapper;

    @Override
    public List<LitemallTopic> getAllBytitle(String title, String subtitle, Integer page, Integer pageSize,String sort, String order) {
        LitemallTopicExample example = new LitemallTopicExample();
        LitemallTopicExample.Criteria criteria = example.createCriteria();
        PageHelper.startPage(page,pageSize);
        criteria.andDeletedEqualTo(false);
        if(title != null) {
            criteria.andTitleLike('%' + title + '%');
        }
        if(subtitle != null) {
            criteria.andSubtitleLike('%' + subtitle + '%');
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        return litemallTopicMapper.selectByExample(example);
    }

    @Override
    public boolean add(LitemallTopic topic) {
        topic.setAddTime(new Date());
        topic.setDeleted(false);
        return litemallTopicMapper.insert(topic) > 0 ? true : false;
    }

    @Override
    public boolean modify(LitemallTopic topic) {
        topic.setUpdateTime(new Date());
        return litemallTopicMapper.updateByPrimaryKey(topic) > 0 ? true : false;
    }

    @Override
    public boolean delete(LitemallTopic topic) {
        topic.setDeleted(true);
        return litemallTopicMapper.updateByPrimaryKey(topic) > 0 ? true : false;
    }

    @Override
    public LitemallTopic getTopicById(Integer id) {
        return litemallTopicMapper.selectByPrimaryKey(id);
    }

    public List<LitemallTopic> queryList(Integer page,Integer size){
        LitemallTopicExample example = new LitemallTopicExample();
        PageHelper.startPage(page,size);
        return litemallTopicMapper.selectByExample(example);
    }

    @Override
    public LitemallTopic findById(Integer id) {
        LitemallTopicExample example = new LitemallTopicExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return litemallTopicMapper.selectOneByExampleWithBLOBs(example);
    }
}
